<template>
  <div class="item-one" ref="itemOne">
    <div class="title">
      <span>优惠券管理</span>
    </div>

    <div class="container">
        <div class="chengetit">
            <div :class="items == 1? 'chengecont' :'chengecont1'" @click="items = 2">
              公域券码核销
            </div>
           
        </div>

        <div v-if="items == 2" style="width:500px;height:210px;margin: 60px auto">
            <el-form :label-position="'top'"  :rules="rules" ref="formLabelAlign" :model="formLabelAlign">
           
             <el-form-item label="请选择公域渠道" prop="name" >
              <el-select v-model="bankType" style="height:50px;width:480px" placeholder="公域渠道">
                    <el-option keys="1" label="抖音" value="1"></el-option>
                    <el-option keys="2" label="美团" value="2"></el-option>
                    <el-option keys="3" label="口碑" value="3"></el-option>
                    <el-option keys="4" label="小红书" value="4"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="请输入优惠券码" prop="name" >
                <el-input style="height:50px;width:480px" v-model="formLabelAlign.name"></el-input>
            </el-form-item>
            <el-form-item label="请输入订单金额"  style="position: relative;" prop="value" >
                <el-input style="height:50px;width:480px" type="number" v-model="formLabelAlign.value"></el-input>
                 <span style="font-size:20px;">元</span>
            </el-form-item>

            <el-form-item label="请选择门店"  v-if="userInfo.rolesId == 5"  style="position: relative;" prop="region">
                  <el-select v-model="formLabelAlign.region"   style="height:50px;width:480px" filterable placeholder="">
                    <el-option
                      v-for="item in options"
                      :key="item.value"
                      :label="item.shopName"
                      :value="item.shopNo">
                    </el-option>
                  </el-select>
            </el-form-item>
            </el-form>
        </div>

        <div v-if="false"  style="width:500px;height:210px;margin: 60px auto">
            <el-form :label-position="'top'"  :rules="rules1" ref="formLabelAlign1" :model="formLabelAlign1">
            <el-form-item label="请输入优惠券码" prop="name1" >
                <el-input style="height:50px" v-model="formLabelAlign1.name"></el-input>
            </el-form-item>
            <el-form-item label="请输入支付订单号" prop="value" >
                <el-input style="height:50px" v-model="formLabelAlign1.value"></el-input>
               
            </el-form-item>
            </el-form>
        </div>

        <div v-if="items == 2" class="chengebutton" @click="chengeclick('formLabelAlign')">
            开始核销
        </div>

    </div>
  </div>
</template>

<script>
import { mdrecord,verification} from "@/api/api";
export default {
  name: "CodeVerification",
  data: function() {
    return {
        items:2,
        userInfo:JSON.parse(sessionStorage.getItem('userInfo')),
        formLabelAlign: {
          name: '',
          value:'',
          region:'',
        },
        formLabelAlign1: {
          name: '',
          value:''
        },
        rules: {
          name: [
            { required: true, message: '请输入优惠券码', trigger: 'blur' },
          ],
           value: [
            { required: true, message: '请输入核销金额', trigger: 'blur' },
          ],
           region: [
            { required: true, message: '请选择门店', trigger: 'change' }
          ],
        },
        rules1: {
          name1: [
            { required: true, message: '请输入优惠券码', trigger: 'blur' },
          ],
           value: [
            { required: true, message: '请输入支付订单号', trigger: 'blur' },
          ],
        },
         options: [],
         deda:''
    };
  },
  created() {
    this.$store.dispatch("getNewNum", 6);
    // console.log(this.$store.state.changableNum);
    // console.log(this.$store.state.showFooter);
    // console.log(this.$store.state.logininfo);
    // console.log('tag', JSON.parse(sessionStorage.getItem('userInfo')))
    this.chengeline()
  },
  methods: {
    //查询门店
    chengeline(){
      if(this.userInfo.rolesId == 5){
        mdrecord({page:1,size:20,merchantId:this.userInfo.mid}).then(res=>{
          this.options = res.data.rows
        })
      }else{

      }

    },
    chengeclick(formName){

 
    var date = new Date()
    var Y = date.getFullYear() + '-'
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
    var D = date.getDate() < 10 ? '0' + date.getDate() + ' ' : date.getDate() + ' '
    var H = date.getHours() + ':'
    var M2 = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'
    var S = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()

    this.deda = Y + M + D + H + M2 + S
           
         this.$refs[formName].validate((valid) => {
          if (valid) {
            if(this.userInfo.rolesId == 5){
              let ss ={
              amount:this.formLabelAlign.value *100,
              channel_type:'10001',
              coupon_code:this.formLabelAlign.name,
              inst_no:'60000001',
              ts:this.deda,
              shop_no:this.formLabelAlign.region,
              write_off_type:0,
            }
            // console.log('ssss', ss)
              verification(ss).then(res=>{
                if(res.data.result_code == 'SUCCESS' && res.data.return_code == 'SUCCESS'){

                  this.$router.push({
                    path: "/verification",
                    query: {
                        suol: '1',
                        order_no:res.data.order_no
                      }
                  });

                  this.formLabelAlign= {
                      name: '',
                       value:'',
                       region:'',
                    }

                }else{
                  this.$router.push({
                    path: "/verification",
                    query: {
                        suol: '2',
                        ddh:this.formLabelAlign.name,
                        return_msg:res.data.return_msg
                      }
                  });
                   this.formLabelAlign= {
                     name: '',
                      value:'',
                      region:'',
                    }
                }

            })

            }else{
              let ss ={
              amount:this.formLabelAlign.value *100,
              channel_type:'10001',
              coupon_code:this.formLabelAlign.name,
              inst_no:'60000001',
              ts:this.deda,
              write_off_type:0,
            }
            // console.log('ssss', ss)
              verification(ss).then(res=>{
                 if(res.data.result_code == 'SUCCESS' && res.data.return_code == 'SUCCESS'){

                  this.$router.push({
                    path: "/verification",
                    query: {
                        suol: '1',
                        order_no:res.data.order_no
                      }
                  });

                  this.formLabelAlign= {
                      name: '',
                       value:'',
                       region:'',
                    }

                }else{
                  this.$router.push({
                    path: "/verification",
                    query: {
                        suol: '2',
                        ddh:this.formLabelAlign.name,
                        return_msg:res.data.return_msg
                      }
                  });
                   this.formLabelAlign= {
                     name: '',
                      value:'',
                      region:'',
                    }
                }
            })

            }
            
          } else {
         
            return false;
          }
        });
      
     
      
      }
 
 
  }
};
</script>
<style scoped lang='less'>
.title {
  background: #fff;
  padding: 10px 10px;
  font-size: 18px;
  font-weight: 500;
  display: flex;
  justify-content: space-between;

  position: relative;
}
.chengetit{
    width: 400px;
    height: 50px;
    margin: 30px auto;
    display: flex; 
}
.chengecont{
    width: 200px;
    height: 50px;
    background-color: white;
    color: #409EFF;
    font-size: 16px;
    box-sizing: border-box;
    border: 1px solid #409EFF ;
    text-align: center;
    line-height: 50px;
    cursor: pointer;
}
.chengecont1{
    width: 350px;
    height: 50px;
    box-sizing: border-box;
    background-color: rgba(122, 187, 255, 1);
    color: white;
    font-size: 16px;
    text-align: center;
    line-height: 50px;
     cursor: pointer;
}

/deep/.el-form-item__label{
    font-size: 20px!important;
}
.el-input /deep/ .el-input__inner {
    height: 50px;
}
.container{
    height: 490px;
}
.chengebutton{
    height: 40px;
    width: 120px;
    background-color: rgba(122, 187, 255, 1);
    color: white;
    text-align: center;
    line-height: 40px;
    margin: 10px auto;
    border-radius: 10px;
     cursor: pointer;
     margin-top: 110px;
}
/deep/.el-input--small .el-input__inner{
  height: 45px;
}

</style>